#include "lrcpage.h"
#include "ui_lrcpage.h"

LrcPage::LrcPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LrcPage)
{
    ui->setupUi(this);

     setWindowFlag(Qt::FramelessWindowHint);

    lrcAnimation = new QPropertyAnimation(this, "geometry", this);
     lrcAnimation->setDuration(250);
     lrcAnimation->setStartValue(QRect(10, 10, width(), height()));
     lrcAnimation->setEndValue(QRect(10, 10+height(), width(), height()));

     connect(ui->hideBtn, &QPushButton::clicked, this, [=]{
      lrcAnimation->start();
      });

     connect(lrcAnimation, &QPropertyAnimation::finished, this, [=]{
      hide();
     });

     ui->hideBtn->setIcon(QIcon(":/xiala.png"));
}

bool LrcPage::parseLrc(const QString &lrcPath)
{
    lrcLines.clear();
    QFile lrcFile(lrcPath);
    if(!lrcFile.open(QFile::ReadOnly))
    {
        qDebug()<<"open the lrcfile:"<<lrcPath;
        return false;
    }
    while(!lrcFile.atEnd())
    {
         QString lrcWord = lrcFile.readLine(1024);
         int left = lrcWord.indexOf('[');
         int right = lrcWord.indexOf(']');

         //解析时间
         qint64 lineTime = 0;
         int start = 0;
         int end = 0;
         QString time = lrcWord.mid(left, right-left+1);
         //解析分钟
        start = 1;
        end = time.indexOf(':');
        lineTime += lrcWord.mid(start, end-start).toInt()*60*1000;
        //解析秒
        start = end + 1;
        end = time.indexOf('.', start);
        lineTime += lrcWord.mid(start, end-start).toInt()*1000;
        //解析毫秒
        start = end + 1;
        end = time.indexOf('.', start);
        lineTime += lrcWord.mid(start, end-start).toInt();

        //解析歌词
        QString word = lrcWord.mid(right+1).trimmed();
        lrcLines.push_back(LyricLine(lineTime, word.trimmed()));
    }
    for(auto word : lrcLines)
    {
        qDebug()<<word.time<<" "<<word.text;
    }
    return true;
}

LrcPage::~LrcPage()
{
    delete ui;
}

void LrcPage::showLrcWord(int time)
{
    int index = getLineLrcWordIndex(time);
    if(-1 == index)
    {
        ui->lineCenter->setText("当前歌曲⽆歌词");
        return;
    }
    else
    {
        ui->line1->setText(getLineLrcWord(index-3));
         ui->line2->setText(getLineLrcWord(index-2));
         ui->line3->setText(getLineLrcWord(index-1));
         ui->lineCenter->setText(getLineLrcWord(index));
         ui->line4->setText(getLineLrcWord(index+1));
         ui->line5->setText(getLineLrcWord(index+2));
         ui->line6->setText(getLineLrcWord(index+3));
    }
}

int LrcPage::getLineLrcWordIndex(qint64 pos)
{
    if(lrcLines.isEmpty())
    {
        qDebug()<<"no word";
        return -1;
    }
    if(lrcLines[0].time > pos)
    {
        return -1;
    }
    if (pos >= lrcLines.last().time)
    {
        return lrcLines.size() - 1;
    }
    for(int i = 1; i < lrcLines.size(); ++i)
    {
        if(pos > lrcLines[i-1].time && pos <= lrcLines[i].time)
        {
            return i-1;
        }
    }
    return lrcLines.size()-1;
}

QString LrcPage::getLineLrcWord(qint64 index)
{
    if(index < 0 || index >= lrcLines.size())
    {
        return " ";
    }
    return lrcLines[index].text;
}

void LrcPage::setMusicName(QString musicname)
{
    ui->musicName->setText(musicname);
}

void LrcPage::setSingerName(QString singername)
{
    ui->musicSinger->setText(singername);
}




